
<?php
// Funcoes de banco uteis

function getAssetValue( $asset, $month, $year )
{
	// Connect do database
	$con=mysqli_connect("localhost","root","assetsdbpass","assetsdb");

	// Check connection
	if( mysqli_connect_errno($con) )
	{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}

	// Reads informations
	$result = mysqli_query($con,"SELECT * FROM assets WHERE month = '" . $month . "' AND year = '" . $year . "' AND name = '" . $asset . "'" );
	$value =  mysqli_fetch_array($result);

	// Fecha o banco
	mysqli_close($con);

	// Check if query returns a value
	if( $value == NULL )
		return NULL;

	return $value['value'];
}

function getAssetsValues( $month, $year )
{
	// Connect do database
	$con=mysqli_connect("localhost","root","assetsdbpass","assetsdb");

	// Check connection
	if( mysqli_connect_errno($con) )
	{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}

	// Reads informations
	$result = mysqli_query($con,"SELECT * FROM assets WHERE month = '" . $month . "' AND year = '" . $year . "'" );

	$assetsValues = array();
	$i = 1;
	while( $row = mysqli_fetch_array($result) )
	{
		$assetsValues[$i][1] = $row['value'];
		$i = $i + 1;
		//echo $row['name'] . " " . $row['value'];
		//echo "<br>";
	}

	// Fecha o banco
	mysqli_close($con);

	return $assetsValues;
}

function getNumberOfRecords()
{
	// Connect do database
	$con = mysqli_connect("localhost", "root", "assetsdbpass", "assetsdb");

	// Check connection
	if( mysqli_connect_errno($con) )
	{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}

	// Reads informations
	$sql = mysqli_query($con, "SELECT COUNT(id) as totalAssets FROM assets" );
	$value =  mysqli_fetch_array($sql);

	// Fecha o banco
	mysqli_close($con);

	// Check if query returns a value
	if( $value == NULL )
		return 0;

	return $value['totalAssets'];
}

function getOldestDate( &$oldestMonth, &$oldestYear )
{
	// Connect do database
	$con = mysqli_connect("localhost", "root", "assetsdbpass", "assetsdb");

	// Check connection
	if( mysqli_connect_errno($con) )
	{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}

	// Reads informations
	$sql = mysqli_query($con, "SELECT * FROM assets ORDER BY year ASC, month ASC" );

	// Divide pelo numero total de ativos
	$row = mysqli_fetch_array($sql);

	// Pega os valores do mes/ano mais antigos do sistema
	$oldestMonth = $row['month'];
	$oldestYear = $row['year'];

	// Fecha o banco
	mysqli_close($con);
} 

function getNewestDate( &$newestMonth, &$newestYear )
{
	// Connect do database
	$con = mysqli_connect("localhost", "root", "assetsdbpass", "assetsdb");

	// Check connection
	if( mysqli_connect_errno($con) )
	{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}

	// Reads informations
	$sql = mysqli_query($con, "SELECT * FROM assets ORDER BY year DESC, month DESC" );

	// Divide pelo numero total de ativos
	$row = mysqli_fetch_array($sql);

	// Pega os valores do mes/ano mais antigos do sistema
	$newestMonth = $row['month'];
	$newestYear = $row['year'];

	// Fecha o banco
	mysqli_close($con);
} 

function updateAssetValue( $asset, $newValue, $month, $year )
{
	// Connect do database
	$con=mysqli_connect("localhost","root","assetsdbpass","assetsdb");

	// Check connection
	if( mysqli_connect_errno($con) )
	{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
		return 0;
	}

	// Reads informations
	$result = mysqli_query($con,"UPDATE assets SET value = " . $newValue ." WHERE month = '" . $month . "' AND year = '" . $year . "' AND name = '" . $asset . "'" );

	// Fecha o banco
	mysqli_close($con);

	return 1;
}

?>
